package ren.steve.ngh.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ren.steve.annotation.Log;
import ren.steve.common.result.HttpStatus;
import ren.steve.common.result.Result;
import ren.steve.enums.BusinessType;
import ren.steve.model.ngh.bean.NghQueryTab;
import ren.steve.ngh.service.NghQueryTabService;

import java.util.List;
import java.util.Map;

/**
 * @Author: CIS-T-13 Liu GuoJia
 * @Description:
 * @Date: Created in 2024/3/25 16:11
 */

@Api(tags = "APIs-NGH-TAB标签页")
@CrossOrigin
@RestController
@RequestMapping("/api/ngh/query/tab")
public class NghQueryTabController {

  @Autowired
  NghQueryTabService nghQueryTabService;

  @ApiOperation("API-查询所有")
  @Log(title = "NGH_数据查询_TAB标签页(所有数据)", businessType = BusinessType.GET)
  @GetMapping("list")
  public Result findList (String user) {
    List<NghQueryTab> list = nghQueryTabService.list(user);
    return Result.ok(list, HttpStatus.OK);
  }
  @ApiOperation("API-添加TAB")
  @Log(title = "NGH_数据查询_添加TAB", businessType = BusinessType.INSERT)
  @PostMapping("add")
  public Result insert (@RequestBody NghQueryTab tab) {
    List<NghQueryTab> tabs = nghQueryTabService.list(tab.getUser());
    if (tabs.size() < 20) {
      NghQueryTab result = nghQueryTabService.insert(tab, tabs);
      return Result.ok(result, HttpStatus.OK);
    } else {
      return Result.build(null, HttpStatus.NGH_QUERY_MAX_TAB);
    }
  }
  @ApiOperation("API-查询所有")
  @Log(title = "NGH_数据查询_TAB标签页(所有数据)", businessType = BusinessType.GET)
  @GetMapping("detail")
  public Result detail (Integer id) {
    NghQueryTab result = nghQueryTabService.selectById(id);
    if (result == null) {
      return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
    }
    return Result.ok(result, HttpStatus.OK);
  }

  @ApiOperation("API-删除TAB")
  @Log(title = "NGH_数据查询_删除TAB", businessType = BusinessType.DELETE)
  @PostMapping("delete")
  public Result delete (@RequestBody NghQueryTab tab) {
    Integer id = Integer.valueOf(tab.getId());
    int result = nghQueryTabService.delete(id);
    if (result != 1) {
      return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
    }
    return Result.ok(null, HttpStatus.OK);
  }
  @ApiOperation("API-批量删除TAB")
  @Log(title = "NGH_数据查询_批量删除TAB", businessType = BusinessType.DELETE)
  @PostMapping("delete/batch")
  public Result deleteBatch (@RequestBody Map<String, String[]> map) {
    if (map.get("ids").equals(null)) {
      return Result.build(null, HttpStatus.NGH_QUERY_TAB_DELETE_BATCH);
    }
    int result = nghQueryTabService.deleteBatch(map.get("ids"));
    if (result == 0) {
      return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
    }
    return Result.ok(null, HttpStatus.OK);
  }
  @ApiOperation("API-更新TAB")
  @Log(title = "NGH_数据查询_更新TAB", businessType = BusinessType.UPDATE)
  @PostMapping("update")
  public Result update (@RequestBody NghQueryTab tab) {
    NghQueryTab result = nghQueryTabService.update(tab);
    if (result == null) {
      return Result.build(null, HttpStatus.SERVER_DAO_ERROR);
    }
    return Result.ok(result, HttpStatus.OK);
  }

}
